$(function () {
    // 获取买家产品库
    var loading = weui.loading('loading', {
        className: 'custom-classname'
    });
    var result = Model.user.buyerProxyGoods();
    for (var i = 0; i < result.goods.length; i++) {
        var item = '<div class="weui-cell list-item">' +
            '        <img class="pruduct-img" src="' + ((result.goods[i].goodsImg.length > 0) ? result.goods[i].goodsImg[0].image : '') + '">' +
            '        <div class="weui-cells">' +
            '            <div class="weui-cell">' +
            '                <h3 class="text-1-6">' + result.goods[i].content + '</h3>' +
            '            </div>' +
            '            <div class="weui-cell">' +
            '                <p class="patron-price">￥ ' + result.goods[i].price + '</p>' +
            '                <span class="text-ffb123">' + result.goods[i].commission + '%佣金</span>' +
            '            </div>' +
            '            <div class="weui-cell">' +
            '                <div class="weui-cell__bd ">' +
            '                    <a href="javascript:;" class="weui-btn weui-btn_mini weui-btn_default cancel-daili" goods-id="' + result.goods[i].id + '">取消代理</a>' +
            '                </div>' +
            '            </div>' +
            '        </div>' +
            '    </div>'
        // 商品为下架状态，显示重新上架按钮
        $('.pruduct-manager').append(item);
    }
    loading.hide();
});

// 取消代理
$(document).on('click', '.cancel-daili', function () {
    var id = $(this).attr('goods-id');
    weui.confirm('确定要取消代理吗？', {
        title: '系统提示',
        buttons: [{
            label: '取消',
            type: 'default',
            onClick: function () {
                console.log('no')
            }
        }, {
            label: '取消代理',
            type: 'primary',
            onClick: function () {
                var result = Model.user.cancelProxy({
                    goodsId: id
                });
                if (result.code == 1) {
                    weui.toast('取消代理成功', {
                        duration: 1500,
                        className: 'custom-classname',
                    });
                    location.reload();
                    return false;
                }
                alert(result.data);
                location.reload();
            }
        }]
    });
});
